package com.mti;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

public class ContainerData extends AsyncTask<String, Void, ArrayList<CityWeather>> {
	static public Context context;
	
	public ContainerData() {}

	@Override
	protected ArrayList<CityWeather> doInBackground(String... params) {
		String locality = params[0];
		String language = params[1];
		
		if (language == "")
			language = "en";

		if (locality == null || locality == "")
			return null;
		
		// On passe par une classe factory pour obtenir une instance de sax
        SAXParserFactory fabrique = SAXParserFactory.newInstance();
        SAXParser parseur = null;
        ArrayList<CityWeather> entries = null;
        
        try {
        	// On "fabrique" une instance de SAXParser
            parseur = fabrique.newSAXParser();
        } catch (ParserConfigurationException e) {
        	e.printStackTrace();
        } catch (SAXException e) {
        	e.printStackTrace();
        }
        
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://www.google.com/ig/api?weather="+locality.replace(" ", "+")+"&hl="+language);
        HttpResponse response;

        DefaultHandler handler = new ParserXMLHandler();
        
        try {
			response = client.execute(request);
            InputStream input = response.getEntity().getContent();

            if(input == null)
                Log.e("erreur android","null");
            else{
                parseur.parse(input, handler);
                // On r�cup�re directement la liste des feeds
                entries = ((ParserXMLHandler) handler).getData();
                
                // Get image bitmap
                URL newurl = new URL(entries.get(0).getIconPath());
				entries.get(0).setIcon(BitmapFactory.decodeStream(newurl.openConnection().getInputStream()));
				
				for (Condition c : entries.get(0).getListCondition()) {
					newurl = new URL(c.getIconPath());
					c.setIcon(BitmapFactory.decodeStream(newurl.openConnection().getInputStream()));
				}
            }
		} catch (ClientProtocolException e1) {
			e1.printStackTrace();
		} catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        // On la retourne l'array list
        return entries;
	}
	
	@Override
	protected void onPostExecute(ArrayList<CityWeather> entries) {
	}
}
